package org.springframework.extcache.cases.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

@Service
public class UserService {

  @Caching(cacheable = { @Cacheable(value = "userCache", key = "#id", cacheManager = "concurrentMapCacheManager"),
      @Cacheable(value = "userCache", key = "#id", cacheManager = "redisCacheManager") })
  public String get(String id) {
    return id;
  }

  @Cacheable(value = "userCache", key = "#id+',expireTime=1'")
  public String getWithExpireTime(String id) {
    return id;
  }

  // @Cacheable(value = "userObjectCache", key =
  // "#user.id+',expireTime=60,params='+#user.toString()")
  // @CacheEvict(value = "userObjectCache", key =
  // "#user.id+',expireTime=60,params='+#user.toString()", beforeInvocation =
  // true)
  @Cacheable(value = "multiUserCache", key = "#user.id", cacheManager = "concurrentMapCacheManager")
  public User add(User user, List<User> list) {
    list.add(user);
    return user;
  }

  // @Cacheable(value = "multiUserCache", key = "#id", cacheManager =
  // "concurrentMapCacheManager")
  public User getById(String id) {
    Map<String, User> users = new HashMap<String, User>();
    User u = new User();
    u.setId("1");
    User u2 = new User();
    u2.setId("2");

    users.put(u.getId(), u);
    users.put(u2.getId(), u2);
    return users.get(id);
  }
}
